﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Configuration;
using System.Web.Configuration;

namespace CashFlow.Web.Common
{
    public class MyConfig
    {
        public static int RootUserId { get; private set; }
        public static int MTCPUserId { get; private set; }
        public static int OrderAmountMin { get; set; }
        public static int OrderAmountMax { get; set; }
        public static int ReleaseOrderHourStart { get; private set; }
        public static int ReleaseOrderHourEnd { get; private set; }
        public static IList<string> ProductNames { get; private set; } = new List<string>();

        static MyConfig()
        {
            {
                int rootUserId = 0;
                bool b = int.TryParse(ConfigurationManager.AppSettings["RootUserId"], out rootUserId);
                if (b)
                {
                    RootUserId = rootUserId;
                }
            }
            {
                int mtcpUserId = 0;
                bool b = int.TryParse(ConfigurationManager.AppSettings["MTCPUserId"], out mtcpUserId);
                if (b)
                {
                    MTCPUserId = mtcpUserId;
                }
            }

            {
                int n = 0;
                bool b = int.TryParse(ConfigurationManager.AppSettings["OrderAmountMin"], out n);
                if (b)
                {
                    OrderAmountMin = n;
                }
            }

            {
                int n = 0;
                bool b = int.TryParse(ConfigurationManager.AppSettings["OrderAmountMax"], out n);
                if (b)
                {
                    OrderAmountMax = n;
                }
            }

            {
                int releaseOrderHourStart = 0;
                bool b = int.TryParse(ConfigurationManager.AppSettings["ReleaseOrderHourStart"]
                    , out releaseOrderHourStart);
                if (b)
                {
                    ReleaseOrderHourStart = releaseOrderHourStart;
                }
            }

            {
                int releaseOrderHourEnd = 0;
                bool b = int.TryParse(ConfigurationManager.AppSettings["ReleaseOrderHourEnd"]
                    , out releaseOrderHourEnd);
                if (b)
                {
                    ReleaseOrderHourEnd = releaseOrderHourEnd;
                }
            }

            {
                string productNames = ConfigurationManager.AppSettings["ProductNames"];
                if (!string.IsNullOrEmpty(productNames))
                {
                    ProductNames = productNames.Split(',').Select(x => x.Trim()).ToList();
                }
            }
        }

        public static AuthenticationSection AuthenticationSection
        {
            get
            {
                var obj = ConfigurationManager.GetSection("system.web/authentication");
                return obj as AuthenticationSection;
            }
        }

        public static SessionStateSection SessionStateSection
        {
            get
            {
                var obj = ConfigurationManager.GetSection("system.web/sessionState");
                return obj as SessionStateSection;
            }
        }
    }
}